﻿using System.Text;

namespace CodeParser.Tokens
{
    public class Precompiler
        : TokenParser
    {

        public override Token GetTokenAt(string source, int index)
        {
            var i = index;
            var sourceLength = source.Length;
            var sb = new StringBuilder();
            var first = true;
            while (i < sourceLength)
            {
                var found = false;
                var currentChar = source[i];
                found = first ? currentChar == '#'
                              : (currentChar >= 'a' && currentChar <= 'z') || (currentChar >= '0' && currentChar <= '9') || currentChar == '_';
                if (found)
                {
                    sb.Append(currentChar);
                    i++;
                }
                if (!found || i == sourceLength)
                {
                    return i > index
                               ? new Token() { Index = index, Type = TokenType.PrecompilerDirective, Value = sb.ToString() }
                               : null;
                }
                first = false;
            }
            return null;
        }

    }
}
